'use strict';

angular.module('oaApp')
  .controller('MainCtrl', function ($scope, $timeout, $http, Auth) {
    $scope.weather = {};
    $scope.date = {};
    $scope.feeds = [];
    $scope.reports = [];
    $scope.projects = [];
    $scope.currentUser = Auth.getCurrentUser();
    $scope.weatherLoading = false;
    $scope.feedsLoading = false;

    $timeout(function () {
      $http.get('/api/shares/weather')
        .success(function (res) {
          $scope.weather = res;
          $scope.weatherLoading = true;
        });
    }, 400);

    $timeout(function () {
      var now = new Date();
      $http.get('/api/timeline?created='+now)
        .success(function (res) {
          $scope.feeds = res;
          $scope.feedsLoading = true;
        });
    }, 400);

    $http.get('/api/projects')
      .success(function (res) {
        $scope.projects = res;
      });
    $http.get('/api/projects/me')
      .success(function (res) {
        $scope.leadcount = res.length;
      });

    $http.get('/api/reports/me?user='+Auth.getCurrentUser()._id)
      .success(function (res) {
        console.log(Auth.getCurrentUser()._id);
        $scope.reports = res;
      });

    // 更新函数
    var updateTime = function() {
      $scope.date.raw = new Date();
      $timeout(updateTime, 1000);
    };
    // 启动更新函数
    updateTime();
  })
  .controller('ProfilesCtrl', function ($scope, $http, Auth, User) {
    $scope.users = User.query();
  });
